//Copyright 2012 Trevin Liberty
//GNU GPL v3
//Author: Trevin Liberty

#include <allegro5\allegro.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_primitives.h>

#include <string.h>
#include <sstream>

#include "Swordsmen.h"
#include "States\SwordsmenOwnedStates.h"

using std::string;
using std::ostringstream;

Slide* Swordsmen::slideWalking = NULL;
Slide* Swordsmen::slideLooking = NULL;
Slide* Swordsmen::slideAttacking = NULL;

Slide* Swordsmen::slideWalkingComp = NULL;
Slide* Swordsmen::slideLookingComp = NULL;;
Slide* Swordsmen::slideAttackingComp = NULL;;

Swordsmen::Swordsmen(int maxHealth, Map* map, AStar* aStar) : Entity(maxHealth, map, Point<int>(64, 64), aStar) {
	stateMachine = new StateMachine<Swordsmen>(this);
	stateMachine->SetCurrentState(WaitS::Instance());

	if(slideWalking == NULL)
		loadImages();

	animator.setSlide(slideLooking, .15);
}

void Swordsmen::loadImages() {
	slideWalking = new Slide();
	slideLooking = new Slide();
	slideAttacking = new Slide();

	slideWalkingComp = new Slide();
	slideLookingComp = new Slide();
	slideAttackingComp = new Slide();

	ALLEGRO_BITMAP* image = NULL;

	slideWalking->image = al_load_bitmap("rsc/Entity/Swordsmen/s_walking.tga");
	slideWalking->slideHeight = 96;
	slideWalking->slideWidth = 96;
	slideWalking->numSlides = 8;

	slideLooking->image = al_load_bitmap("rsc/Entity/Swordsmen/s_looking.tga");
	slideLooking->slideHeight = 96;
	slideLooking->slideWidth = 96;
	slideLooking->numSlides = 13;

	slideAttacking->image = al_load_bitmap("rsc/Entity/Swordsmen/s_attacking.tga");
	slideAttacking->slideHeight = 128;
	slideAttacking->slideWidth = 128;
	slideAttacking->numSlides = 13;

	slideWalkingComp->image = al_load_bitmap("rsc/Entity/Swordsmen/k_walking.tga");
	slideWalkingComp->slideHeight = 96;
	slideWalkingComp->slideWidth = 96;
	slideWalkingComp->numSlides = 8;

	slideLookingComp->image = al_load_bitmap("rsc/Entity/Swordsmen/k_looking.tga");
	slideLookingComp->slideHeight = 96;
	slideLookingComp->slideWidth = 96;
	slideLookingComp->numSlides = 9;

	slideAttackingComp->image = al_load_bitmap("rsc/Entity/Swordsmen/k_attacking.tga");
	slideAttackingComp->slideHeight = 96;
	slideAttackingComp->slideWidth = 96;
	slideAttackingComp->numSlides = 13;
}

//bool Swordsmen::outOfRangeGuardPoint() {
//	if(sqrt(pow((pos.x - guardPoint.x), 2) + pow((pos.y - guardPoint.y), 2)) >= 15)
//		return true;
//	return false;
//}

void Swordsmen::update() {
	if(hasTarget() && target->getHealthPoints() <= 0) {
		removeTarget();
	}

	stateMachine->Update();
	updatePath();

	if(healthPoints <= 0) {
		remove = true;
		return;
	}

	if(prevDir != direction ||
		stateMachine->CurrentState() != stateMachine->PreviousState()) {

		if(team == 0) {
			if(stateMachine->isInState(*WaitS::Instance()) || 
				(isBlocked() && stateMachine->isInState(*MoveS::Instance())))
				animator.setSlide(slideLooking, .15);
			else if(stateMachine->isInState(*MoveS::Instance()))
				animator.setSlide(slideWalking, .1);
			else if(stateMachine->isInState(*AttackS::Instance()))
				animator.setSlide(slideAttacking, attackSpeed/16.0);
		}
		else {
			if(stateMachine->isInState(*WaitS::Instance()) || 
				(isBlocked() && stateMachine->isInState(*MoveS::Instance())))
				animator.setSlide(slideLookingComp, .15);
			else if(stateMachine->isInState(*MoveS::Instance()))
				animator.setSlide(slideWalkingComp, .1);
			else if(stateMachine->isInState(*AttackS::Instance()))
				animator.setSlide(slideAttackingComp, attackSpeed/16.0);
		}
		switch(direction) {
		case E:
			animator.setDirection(3);
			break;
		case N:
			animator.setDirection(1);
			break;
		case S:
			animator.setDirection(2);
			break;
		case W:
			animator.setDirection(4);
			break;
		case NE:
			animator.setDirection(5);
			break;
		case NW:
			animator.setDirection(6);
			break;
		case SE:
			animator.setDirection(8);
			break;
		case SW:
			animator.setDirection(7);
			break;	
		}
		prevDir = direction;
	}
}

void Swordsmen::draw(Point<float> cameraPos) {
	ALLEGRO_BITMAP* img = animator.getImage();

	Point<float> p = pos;
	p.x -= cameraPos.x;
	p.y -= cameraPos.y;

	if(this->isSelected())
		al_draw_ellipse(p.x, p.y, 13, 11, al_map_rgb(50, 255, 50), 3);

	if(stateMachine->isInState(*AttackS::Instance()) && team == 0) 
		al_draw_scaled_bitmap(img, 0, 0, 128, 128,
			p.x - 43, p.y - 43, 86, 86, 0);
	else
		al_draw_scaled_bitmap(img, 0, 0, 96, 96,
			p.x - 32, p.y - 32, 64, 64, 0);

	if(selected)
		drawHealth(cameraPos, Point<int>(-16, 18), 32);
}

void Swordsmen::deleteImages() {
	if(slideWalking == NULL)
		return;

	al_destroy_bitmap(slideWalking->image);
	al_destroy_bitmap(slideLooking->image);
	al_destroy_bitmap(slideAttacking->image);
	al_destroy_bitmap(slideWalkingComp->image);
	al_destroy_bitmap(slideLookingComp->image);
	al_destroy_bitmap(slideAttackingComp->image);

	delete slideWalking;
	delete slideLooking;
	delete slideAttacking;
	delete slideWalkingComp;
	delete slideLookingComp;
	delete slideAttackingComp;

	slideWalking = NULL;
}